package com.tv189.springbatchtest.stopsteplisten;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @ClassName: StopListenerJob
 * @Description: 停止步骤监听器
 * @Author: 19202
 * @Date: 2025/6/2 12:55
 * @Version 1.0
 **/
@EnableBatchProcessing
@SpringBootApplication
public class StopListenerJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    private int readCount = 100;

    @Bean
    public Tasklet tasklet1() {
        return (stepContribution, chunkContext) -> {
            for (int i = 1; i <= readCount; i++) {
                System.out.println("-----step1执行--------" + i + "------------");
                ResourceCount.readCount ++;
            }

            if (ResourceCount.readCount != ResourceCount.totalCount) {
                chunkContext.getStepContext().getStepExecution().setTerminateOnly();
            }
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet tasklet2() {
        return (stepContribution, chunkContext) -> {
            System.out.println("--------step2执行了-----------");
            System.out.println("readcount：" + ResourceCount.readCount + "，totalCount:" + ResourceCount.totalCount);
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public StopStepListener stopStepListener() {
        return new StopStepListener();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet(tasklet1())
                .listener(stopStepListener())
                // 允许执行完成后重新执行  因为执行step1的时候
                .allowStartIfComplete(true)
                .build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet(tasklet2())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job-stop-job")
                .start(step1())
                .next(step2())
                //.on("STOPPED").stopAndRestart(step1())
                //.from(step1()).on("*").to(step2())
                //.end()
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(StopListenerJob.class, args);
    }
}
